package com.empresa.androidchkapp;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.empresa.androidchkapp.pojos.ChecklistItem;

public class CustomAdapter extends ArrayAdapter<ChecklistItem> {

	private final List<ChecklistItem> list;
	private Context context;
	private LayoutInflater inflater;
	private int resource;
	HashMap<String, String> SavedData = new HashMap<String, String>();

	public CustomAdapter(Context context, int resourceId,
			List<ChecklistItem> objects) {
		super(context, R.layout.row, objects);
		this.context = context;
		this.resource = resourceId;
		inflater = LayoutInflater.from(context);
		this.list = objects;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;
		final ChecklistItem store = getItem(position);

		if (convertView == null) {
			convertView = inflater.inflate(R.layout.row, null);
			holder = new ViewHolder();

			convertView.setTag(holder);

			holder.texto = (TextView) convertView.findViewById(R.id.itemchk);
			// holder.idChk = (TextView)
			// convertView.findViewById(R.id.itemchkId);
			String mItem = (String) list.get(position).getNome();
			holder.texto.setText(mItem);

			holder.rbGroup = (RadioGroup) convertView.findViewById(R.id.rg1);
			holder.btn1 = (RadioButton) convertView.findViewById(R.id.conforme);
			holder.btn2 = (RadioButton) convertView
					.findViewById(R.id.naoconforme);
			holder.btn3 = (RadioButton) convertView
					.findViewById(R.id.naoaplica);
			
			holder.obs = (EditText) convertView.findViewById(R.id.editTextNC);
			
			holder.botaoDeAjuda = (ImageButton) convertView.findViewById(R.id.imageButton1);
			
			final String ajuda = (String) list.get(position).getAjuda();
			
			holder.botaoDeAjuda.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {

					Toast.makeText(context ,ajuda, Toast.LENGTH_SHORT).show();
				}
			});

			holder.rbGroup
					.setOnCheckedChangeListener(new OnCheckedChangeListener() {

						public void onCheckedChanged(RadioGroup group,
								int checkedId) {

							Integer pos = (Integer) group.getTag();
							ChecklistItem element = list.get(pos);

							switch (checkedId) {
							case R.id.conforme:

								element.current = ChecklistItem.ANSWER_ONE_SELECTED;
								break;

							case R.id.naoconforme:

								element.current = ChecklistItem.ANSWER_TWO_SELECTED;
								break;

							case R.id.naoaplica:
								element.current = ChecklistItem.ANSWER_THREE_SELECTED;
								break;
							default:
								element.current = ChecklistItem.NONE; // Something
																		// was
																		// wrong
																		// set
																		// to
																		// the
																		// default
							}
						}
					});

		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		


		holder.rbGroup.setTag(new Integer(position)); // I passed the current position as a tag
		holder.texto.setText(list.get(position).getNome());
		
		holder.botaoDeAjuda.setTag(position);  //to get the orignal position later in onClick() of button
		holder.botaoDeAjuda.setOnClickListener(new View.OnClickListener() {  
		          public void onClick(View v) {   
		                 int pos = (Integer) v.getTag();  //the real and updated position
		                 Toast.makeText(context ,list.get(pos).getAjuda(), Toast.LENGTH_SHORT).show();
		          }       
		});  
	
		holder.obs.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				 if (!hasFocus) {
		                EditText et = (EditText) v.findViewById(R.id.editTextNC);
		                list.get(position).setObs(et.getText().toString());
		            }
			}
		});
		
//		final EditText editTxt = (EditText) convertView.findViewById(R.id.editTextNC);
//		if(SavedData.containsKey(position)) {
//		    editTxt.setText(SavedData.get(position).toString());
//
//		}
//		
//		holder.obs.addTextChangedListener(new TextWatcher() {
//			
//			@Override
//			public void onTextChanged(CharSequence s, int start, int before, int count) {
//				// TODO Auto-generated method stub
//				
//			}
//			
//			@Override
//			public void beforeTextChanged(CharSequence s, int start, int count,
//					int after) {
//				// TODO Auto-generated method stub
//				
//			}
//			
//			@Override
//			public void afterTextChanged(Editable s) {
//				//Save your Changed Text to HashMap using tag of edittext
//                SavedData.add(editTxt.getTag().toString(),s.toString());
//				
//			}
//		});
		
		
		holder.obs.setText(list.get(position).getObs());

		if (list.get(position).current != ChecklistItem.NONE) {
			RadioButton r = (RadioButton) holder.rbGroup.getChildAt(list
					.get(position).current);
			r.setChecked(true);
		} else {
			holder.rbGroup.clearCheck(); // This is required because although
											// the Model could have the current
											// position to NONE you could be
											// dealing with a previous row where
											// the user already picked an
											// answer.

		}
		
		LinearLayout layoutView = new LinearLayout(this.context);
        layoutView.setOrientation(LinearLayout.HORIZONTAL);
        
		
		return convertView;
	}
	


	class ViewHolder {
		//private TextView textoAjuda;
		private TextView texto;
		RadioGroup rbGroup;
		RadioButton btn1;
		RadioButton btn2;
		RadioButton btn3;
		EditText obs;
		ImageButton botaoDeAjuda;
	}

}
